<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Branch_mapper extends Base_mapper{
	protected $object_class = 'Branch_object';
	
	protected $table = 'branches';
	
	protected $primary_key = 'id';
	
	protected function doBuildObject($row){
		return new Branch_object($row->id, $row->vendors_id, $row->name, $row->phone, $row->addresses_id);
	}
	protected function doSave($obj){
		//check for dependency
		if(is_null($obj->getVendors_id())){
			throw new Exception("Dependency key not found, please check if the dependency key is set");			
		}
		parent::doSave($obj);
	} 
	protected function doDelete($obj){
		//TODO: handle primary_branch issue, might not need to worry about it
		//handle all the delete for the relationship tables
		$tables = array('coupons_branches', 'addresses');
		$this->db->where('branches_id', $obj->getId());
		$this->db->delete($tables);
		parent::doDelete($obj);
	}
	protected function doInsert(Branch_object $obj){
		$data = array(
				'vendors_id' => $obj->getVendors_id(),
				'name'=>$obj->getName(),
				'phone'=>$obj->getPhone(),
				'addresses_id' => $obj->getAddresses_id()
		);
		$this->db->insert($this->table, $data);
		parent::doInsert($obj);
	}
	protected function doUpdate($obj){
				$data = array(
				'vendors_id' => $obj->getVendors_id(),
				'name'=>$obj->getName(),
				'phone'=>$obj->getPhone(),
				'addresses_id' => $obj->getAddresses_id()
		);
					$this->db->where('id', $obj->getId());
		$this->db->update($this->table, $data);
		parent::doUpdate($obj);
	}
}